package cn.owng.cli.db;

import cn.owng.cli.Env;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import picocli.CommandLine;

import java.sql.Connection;
import java.util.concurrent.Callable;

/**
 * 微信公众号请关注: 开源螺丝钉
 * <br>
 * 码云请关注: <a href="https://gitee.com/xiyoufang">https://gitee.com/xiyoufang</a>
 * <br>
 * 哔哩哔哩请关注: <a href="https://space.bilibili.com/438927834">https://space.bilibili.com/438927834</a>
 *
 * @author xiyoufang
 */
@CommandLine.Command(name = "create", description = "创建数据库")
@Slf4j
public class Create implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        Env.Db db = Env.db();
        try (HikariDataSource ds = new HikariDataSource()) {
            ds.setJdbcUrl(db.getUrl());
            ds.setUsername(db.getUsername());
            ds.setPassword(db.getPassword());
            Connection con = ds.getConnection();
            String sql = "create database " + db.getDatabase() + " charset utf8mb4";
            log.info("执行 SQL: {}", sql);
            con.prepareStatement(sql).execute();
        }
        return 0;
    }
}
